#INITSEG = 0x9000
#SYSSEG  = 0x1000
#SETUPSEG = 0x9020
#.section .data

.include "boot.cfg"
.code16
.text
.globl _start_setup

_start_setup:
     movw %cs, %ax #$INITSEG, %ax #$SETUPSEG, %ax
     movw %ax, %ds
     movw %ax, %es
     #movw %ax, %ss
   movw $setup_msg0, %ax
   movw %ax, %bp
    movw $0x01301, %ax
    movw $0x0c, %bx
    movw $16, %cx
    movb $5, %dh
    movb $0, %dl
    int  $0x010

#   movw $13, %cx
#   movb $5, %dh
#     call print

     movw VBEMODE, %bx
     addw $0x4000, %bx
     movw $0x4f02, %ax
     int $0x10
     movw $INITSEG, %ax
     movw %ax, %ds
 
     movw $8, (16)
     movw $1024, (18)
     movw $768, (20)
     #movl $0xfd000000, (22)

     #call dsply
#here:
#     jmp here

     movb $0x03, %ah
     xor  %bh, %bh
     int $0x10
     movw %dx, (0)
     movb $0x88, %ah
     int $0x15
     movw %ax, (2)

     movb $0x0f, %ah
     int $0x10
     movw %bx, (4)
     movw %ax, (6)
     movb $0x12, %ah
     movb $0x10, %bl
     int $0x10
     movw %ax, (8)
     movw %bx, (10)
     movw %cx, (12)
     movw $0x5019, (14)

     movw $0x0000, %ax
     movw %ax, %ds
     ldsw (4*0x41), %si
     movw $INITSEG, %ax
     movw %ax, %es
     movw $0x0080, %di
     movw $0x10, %cx
     rep
     movsb

     /* 获取hd1数据*/
     movw $0x0000, %ax
     movw %ax, %ds
     ldsw (4*0x46), %si
     movw $INITSEG, %ax
     movw %ax, %es
     movw $0x0090, %di
     movw $0x10, %cx
     rep
     movsb


     movw $0x1500, %ax
     movb $0x81, %dl
     int $0x13
     jc no_disk1
     cmpb $3, %ah
     je   is_disk1
no_disk1:
     movw $INITSEG, %ax
     movw %ax, %es
     movw $0x0090, %di
     movw $0x10, %cx
     movw $0x00, %ax
     rep
     stosb

is_disk1:

#     movw $SETUPSEG, %ax
#     movw %ax, %ds
#     movw %ax, %es
#     ##movw %ax, %ss
#   movw $setup_msg, %ax
#   movw $16, %cx
#   movb $6, %dh
#   movb $0, %dl
#     call print

#   movw $0x1000, %ax
#   movw %ax, %es
#   movw $0, %ax
#   movw $4, %cx
#   movb $9, %dh
#   movb $0, %dl
#     call print
#   movw $SETUPSEG, %ax
#   movw %ax, %ds
#   movw %ax, %es
   #movw %ax, %ss

      cli
      movw $0x0000, %ax
      cld
#      movw $SETUPSEG, %ax
#      movw %ax, %ds
#      movw %ax, %es
#      jmp end_move
do_move:
      movw %ax, %es
      addw $0x1000, %ax
      cmpw $0x9000, %ax
      jz   end_move
      movw %ax, %ds
      subw %di, %di
      subw %si, %si
      movw $0x8000, %cx

      rep
      movsw
      jmp do_move

end_move:
#     movw $SETUPSEG, %ax
#     movw %ax, %ds
     #movw %ax, %es
     #movw %ax, %ss

#   movw $0, %ax
#   movw %ax, %es 
#   movw $0, %ax
#   movw $8, %cx
#   movb $12, %dh
#   movb $0, %dl
#     call print

#     movw %cs, %ax
#     movw %ax, %ds
#     movw %ax, %es
#     ##movw %ax, %ss
      movw $0xb800, %ax
      movw %ax, %gs
      movb $0xf, %ah
      movb $0x41, %al
      movl $0x100, %edi
      movw %ax, %gs:(%di)

      movw $SETUPSEG, %ax
      movw %ax, %ds
      lgdt gdt_48

      call empty_8042
      movb $0xD1, %al
      outb %al, $0x64
      call empty_8042
      movb $0xDF, %al
      outb %al, $0x60
      call empty_8042

    movb $0x11, %al
    outb %al,  $0x20
    .word 0x00eb, 0x00eb
    outb %al,  $0xA0
    .word 0x00eb, 0x00eb
    movb $0x20, %al
    outb %al, $0x21
    .word 0x00eb, 0x00eb
    movb $0x28, %al
    outb %al,   $0xA1
    .word 0x00eb, 0x00eb
    movb $0x04, %al
    outb %al, $0x21
    .word 0x00eb, 0x00eb
    #movb $0x02, %al
    outb %al, $0xA1

    .word 0x00eb, 0x00eb
    movb $0x01, %al
    outb %al, $0x21
    .word 0x00eb, 0x00eb
    outb %al, $0xA1
    .word 0x00eb, 0x00eb
    movb $0xff, %al
    outb %al, $0x21
    .word 0x00eb, 0x00eb
    movb $0xff, %al
    outb %al, $0xA1

      movl %cr0, %eax
      xorb $1, %al
      movl %eax, %cr0


#here:
#     jmp here
     
#      movw $1, %ax
#      lmsw %ax

      .byte 0x66, 0xea     #0x66 扩展指令，0xea 跳转指令
      .long 0x0            #目的地址：0x8:0x0，0x8为代码段选择子,代码段基地址0
      .word 0x8            #它是head模块所在的位置

empty_8042:
      .word 0x00eb, 0x00eb
      inb $0x64, %al
      testb $2, %al
      jnz empty_8042
      ret

dsply:
    movl $0, %ecx        # 初始化计数器为 0

    movl $0xfcf70000, %eax
    movb $15, (%eax)
    movl $1024, %ecx
    imull $768, %ecx
    subl $1, %ecx
    addl %ecx, %eax
    movb $15, (%eax)
    subl $500000, %eax
    movl $0, %ecx
loop_start:
    cmp $100, %ecx        # 比较计数器和 10
    je loop_end          # 如果计数器等于 10，跳转到循环结束

    movb $15, (%eax)
    incl %ecx            # 计数器加 1
    incl %eax
    jmp loop_start       # 跳转到循环开始

loop_end:
     ret

gdt:
      .word 0,0,0,0

      .word 0x07ff   
      .word 0x0000
      .word 0x9a00   
      .word 0x00c0

      .word 0x07ff
      .word 0x0000
      .word 0x9200
      .word 0x00c0

      .word 0xffff
      .word 0x8000
      .word 0x920b
      .word 0x00c0
gdt_48:
      .word 0x800
      .word 512+gdt, 0x9

setup_msg0:
     .ascii "entered Setup"

setup_msg:
     .ascii "Setup is running"
VBEMODE:
      .word 0x105
#.include "print.S"
